---
layout: "default"
title: "String.UnicodeScalarView.UnicodeScalarIndex"
description: "Swift documentation for extension 'String.UnicodeScalarView.UnicodeScalarIndex'."
keywords: "String.UnicodeScalarView.UnicodeScalarIndex,extension,swift,documentation,samePosition,samePosition,samePosition"
root: "/v3.0"
---

<div class="intro-declaration"><code class="language-swift">extension String.UnicodeScalarView.UnicodeScalarIndex</code></div>


<table class="standard">



<tr>
<th>Import</th>
<td><code class="language-swift">import Swift</code></td>
</tr>

</table>


<h3>Initializers</h3>
<div class="declaration" id="init_within_">
<a class="toggle-link" data-toggle="collapse" href="#comment-init_within_">init(<wbr>_:<wbr>within:)</a><div class="comment collapse" id="comment-init_within_"><div class="p">
    <p>Creates an index in the given Unicode scalars view that corresponds
exactly to the specified string position.</p>

<p>The following example converts the position of the teacup emoji (<code>&quot;🍵&quot;</code>)
into its corresponding position in the string&#39;s <code>unicodeScalars</code> view.</p>

<pre><code class="language-swift">let cafe = &quot;Café 🍵&quot;
let characterIndex = cafe.characters.index(of: &quot;🍵&quot;)!
let scalarIndex = String.UnicodeScalarView.Index(characterIndex, within: cafe.unicodeScalars)

print(cafe.unicodeScalars.suffix(from: scalarIndex))
// Prints &quot;🍵&quot;</code></pre>

<p><strong>Parameters:</strong>
  <strong>characterIndex:</strong> A position in a <code>CharacterView</code> instance.
    <code>characterIndex</code> must be an element of
    <code>String(utf8).characters.indices</code>.
  <strong>utf8:</strong> The <code>UTF8View</code> in which to find the new position.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init(_ characterIndex: Index, within unicodeScalars: String.UnicodeScalarView)</code>

    </div></div>
</div>
<div class="declaration" id="init_-utf8index-within_-string-unicodescalarview">
<a class="toggle-link" data-toggle="collapse" href="#comment-init_-utf8index-within_-string-unicodescalarview">init?(<wbr>_:<wbr> UTF8Index, within: String.UnicodeScalarView)</a><div class="comment collapse" id="comment-init_-utf8index-within_-string-unicodescalarview"><div class="p">
    <p>Creates an index in the given Unicode scalars view that corresponds
exactly to the specified <code>UTF8View</code> position.</p>

<p>If the position passed as <code>utf8Index</code> doesn&#39;t have an exact corresponding
position in <code>unicodeScalars</code>, the result of the initializer is <code>nil</code>.
For example, an attempt to convert the position of a UTF-8 continuation
byte returns <code>nil</code>.</p>

<p><strong>Parameters:</strong>
  <strong>utf8Index:</strong> A position in the <code>utf8</code> view of the <code>characters</code>
    parameter.
  <strong>unicodeScalars:</strong> The <code>UnicodeScalarView</code> instance referenced by both
    <code>utf8Index</code> and the resulting index.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init?(_ utf8Index: UTF8Index, within unicodeScalars: String.UnicodeScalarView)</code>

    </div></div>
</div>
<div class="declaration" id="init_-utf16index-within_-string-unicodescalarview">
<a class="toggle-link" data-toggle="collapse" href="#comment-init_-utf16index-within_-string-unicodescalarview">init?(<wbr>_:<wbr> UTF16Index, within: String.UnicodeScalarView)</a><div class="comment collapse" id="comment-init_-utf16index-within_-string-unicodescalarview"><div class="p">
    <p>Creates an index in the given Unicode scalars view that corresponds
exactly to the specified <code>UTF16View</code> position.</p>

<p>The following example finds the position of a space in a string&#39;s <code>utf16</code>
view and then converts that position to an index in the the string&#39;s
<code>unicodeScalars</code> view:</p>

<pre><code class="language-swift">let cafe = &quot;Café 🍵&quot;

let utf16Index = cafe.utf16.index(of: 32)!
let scalarIndex = String.UnicodeScalarView.Index(utf16Index, within: cafe.unicodeScalars)!

print(String(cafe.unicodeScalars.prefix(upTo: scalarIndex)))
// Prints &quot;Café&quot;</code></pre>

<p>If the position passed in <code>utf16Index</code> doesn&#39;t have an exact
corresponding position in <code>unicodeScalars</code>, the result of the
initializer is <code>nil</code>. For example, an attempt to convert the position of
the trailing surrogate of a UTF-16 surrogate pair fails.</p>

<p><strong>Parameters:</strong>
  <strong>utf16Index:</strong> A position in the <code>utf16</code> view of the <code>characters</code>
    parameter.
  <strong>unicodeScalars:</strong> The <code>UnicodeScalarView</code> instance referenced by both
    <code>utf16Index</code> and the resulting index.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init?(_ utf16Index: UTF16Index, within unicodeScalars: String.UnicodeScalarView)</code>

    </div></div>
</div>





<h3>Instance Methods</h3>
<div class="declaration" id="func-sameposition-in_-string">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-sameposition-in_-string">func samePosition(<wbr>in: String)</a>
        
<div class="comment collapse" id="comment-func-sameposition-in_-string"><div class="p">
    <p>Returns the position in the given string that corresponds exactly to this
index.</p>

<p>This index must be a valid index of <code>characters.unicodeScalars</code>.</p>

<p>This example first finds the position of a space (UTF-8 code point <code>32</code>)
in a string&#39;s <code>utf8</code> view and then uses this method find the same position
in the string.</p>

<pre><code class="language-swift">let cafe = &quot;Café 🍵&quot;
let i = cafe.unicodeScalars.index(of: &quot;🍵&quot;)
let j = i.samePosition(in: cafe)!
print(cafe.suffix(from: j))
// Prints &quot;🍵&quot;</code></pre>

<p><strong><code>characters</code>:</strong>  The string to use for the index conversion.
<strong>Returns:</strong> The position in <code>characters</code> that corresponds exactly to
  this index. If this index does not have an exact corresponding
  position in <code>characters</code>, this method returns <code>nil</code>. For example,
  an attempt to convert the position of a UTF-8 continuation byte
  returns <code>nil</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func samePosition(in characters: String) -&gt; Index?</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-sameposition-in_-string-utf8view">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-sameposition-in_-string-utf8view">func samePosition(<wbr>in: String.UTF8View)</a>
        
<div class="comment collapse" id="comment-func-sameposition-in_-string-utf8view"><div class="p">
    <p>Returns the position in the given UTF-8 view that corresponds exactly to
this index.</p>

<p>The index must be a valid index of <code>String(utf8).unicodeScalars</code>.</p>

<p>This example first finds the position of the character <code>&quot;é&quot;</code> and then uses
this method find the same position in the string&#39;s <code>utf8</code> view.</p>

<pre><code class="language-swift">let cafe = &quot;Café&quot;
if let i = cafe.unicodeScalars.index(of: &quot;é&quot;) {
    let j = i.samePosition(in: cafe.utf8)
    print(Array(cafe.utf8.suffix(from: j)))
}
// Prints &quot;[195, 169]&quot;</code></pre>

<p><strong><code>utf8</code>:</strong>  The view to use for the index conversion.
<strong>Returns:</strong> The position in <code>utf8</code> that corresponds exactly to this index.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func samePosition(in utf8: String.UTF8View) -&gt; String.UTF8View.Index</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-sameposition-in_-string-utf16view">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-sameposition-in_-string-utf16view">func samePosition(<wbr>in: String.UTF16View)</a>
        
<div class="comment collapse" id="comment-func-sameposition-in_-string-utf16view"><div class="p">
    <p>Returns the position in the given UTF-16 view that corresponds exactly to
this index.</p>

<p>The index must be a valid index of <code>String(utf16).unicodeScalars</code>.</p>

<p>This example first finds the position of the character <code>&quot;é&quot;</code> and then uses
this method find the same position in the string&#39;s <code>utf16</code> view.</p>

<pre><code class="language-swift">let cafe = &quot;Café&quot;
if let i = cafe.characters.index(of: &quot;é&quot;) {
    let j = i.samePosition(in: cafe.utf16)
    print(cafe.utf16[j])
}
// Prints &quot;233&quot;</code></pre>

<p><strong><code>utf16</code>:</strong>  The view to use for the index conversion.
<strong>Returns:</strong> The position in <code>utf16</code> that corresponds exactly to this index.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func samePosition(in utf16: String.UTF16View) -&gt; String.UTF16View.Index</code>
    
    
</div></div>
</div>


